// mircoApp/coupon/checkList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: ['a'],
    id: '',
    lisinfo: '',
    orTap:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(",");
        console.log(info_arr);
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split("=");
          console.log(chil_arr,'chil_arr');
          if (chil_arr[0] == "id") {
            this.setData({
              id:chil_arr[1],
            });
          }
        }
      }
    }
    console.log(this.data.id);
    // if(options.id) {
    //   this.setData({
    //     id:options.id
    //   })
    // }
    this.checkStatus(this.data.id);
    
  },
  // 优惠券二维码轮询
  checkStatus(id) {
    let urlStr = app.getNetAddresss('plugin.write-off-coupons.Frontend.controllers.code.get-status');
    app._postNetWork({
      url: urlStr,
      data: { record_id: id },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          if(!res.data.status) {
            this.getList(id);
            
          } else {
            wx.showToast({
              title: '已核销',
              duration: 2000,
              icon: 'none'
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getList(id) {
    let urlStr = app.getNetAddresss('plugin.write-off-coupons.Frontend.controllers.write-off.index');
    app._postNetWork({
      url: urlStr,
      data: { record_id:id },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            lisinfo: res.data,
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  init(){
    this.setData({
      orTap:true
    })
  },
  tapSure() {
    if(app._isTextEmpty(this.data.lisinfo)){
      return;
    }
    let urlStr = app.getNetAddresss('plugin.write-off-coupons.Frontend.controllers.write-off.handle');
    app._postNetWork({
      url: urlStr,
      data: { record_id:this.data.id },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
          this.setData({
            orTap:false
          })
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})